<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="理财名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入理财名" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <!-- <el-form-item label="最大金额" prop="maxAmount">
    <el-input
      v-model="queryParams.maxAmount"
      placeholder="请输入最大金额"
      clearable
      @keyup.enter.native="handleQuery"
    />
  </el-form-item>
  <el-form-item label="利率" prop="interestRate">
    <el-input
      v-model="queryParams.interestRate"
      placeholder="请输入利率"
      clearable
      @keyup.enter.native="handleQuery"
    />
  </el-form-item>
  <el-form-item label="收益周期" prop="period">
    <el-input
      v-model="queryParams.period"
      placeholder="请输入收益周期"
      clearable
      @keyup.enter.native="handleQuery"
    />
  </el-form-item>
  <el-form-item label="是否被删除 0 未删除 1 已删除" prop="deleted">
    <el-input
      v-model="queryParams.deleted"
      placeholder="请输入是否被删除 0 未删除 1 已删除"
      clearable
      @keyup.enter.native="handleQuery"
    />
  </el-form-item>
  <el-form-item label="合同模板编码" prop="contractTemplateCode">
    <el-input
      v-model="queryParams.contractTemplateCode"
      placeholder="请输入合同模板编码"
      clearable
      @keyup.enter.native="handleQuery"
    />
  </el-form-item> -->
<!--      <el-form-item label="理财状态" prop="status">
        <dict-select :options="dict.type.financial_line_stauts" v-model="queryParams.status"
                     placeholder="请选择审核状态"
                     @change="financialStatusChange"
        />
      </el-form-item>-->
      <el-form-item label="限制用户等级" prop="limitUserLevel">
        <el-select class="modal-input" v-model="queryParams.limitUserLevel" @change="queryParamsULChange">
          <el-option v-for="(item, index) in userLevelList" :key="item.id" :label="item.name"
                     :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="开始时间" prop="startDate">
    <el-date-picker clearable
      v-model="queryParams.startDate"
      type="date"
       value-format="timestamp"
      placeholder="请选择开始时间">
    </el-date-picker>
  </el-form-item>
  <el-form-item label="周期结束时间" prop="finishTime">
    <el-date-picker clearable
      v-model="queryParams.finishTime"
      type="date"
       value-format="timestamp"
      placeholder="请选择周期结束时间">
    </el-date-picker>
  </el-form-item>
  <el-form-item label="实际购买人数" prop="buyUser">
    <el-input
      v-model="queryParams.buyUser"
      placeholder="请输入实际购买人数"
      clearable
      @keyup.enter.native="handleQuery"
    />
  </el-form-item>
  <el-form-item label="虚拟购买人数" prop="dummyUser">
    <el-input
      v-model="queryParams.dummyUser"
      placeholder="请输入虚拟购买人数"
      clearable
      @keyup.enter.native="handleQuery"
    />
  </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['car:financial:add']"
        >新增
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
    <el-button
      type="success"
      plain
      icon="el-icon-edit"
      size="mini"
      :disabled="single"
      @click="handleUpdate"
      v-hasPermi="['car:financial:edit']"
    >修改</el-button>
  </el-col>
  <el-col :span="1.5">
    <el-button
      type="danger"
      plain
      icon="el-icon-delete"
      size="mini"
      :disabled="multiple"
      @click="handleDelete"
      v-hasPermi="['car:financial:remove']"
    >删除</el-button>
  </el-col>
  <el-col :span="1.5">
    <el-button
      type="warning"
      plain
      icon="el-icon-download"
      size="mini"
      @click="handleExport"
      v-hasPermi="['car:financial:export']"
    >导出</el-button>
  </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="financialList">
      <el-table-column label="理财名" align="center" prop="name" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="封面图" align="center" prop="cover" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <oss-image-preview :model="'text'" :src="scope.row.cover"/>
        </template>
      </el-table-column>
      <el-table-column label="最大金额" align="center" prop="maxAmount" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ formatter(scope.row, 'maxAmount') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="利率" align="center" prop="interestRate" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope"> {{ scope.row.interestRate.toFixed(2)*100 }} %</template>
      </el-table-column>
      <el-table-column label="收益周期" align="center" prop="period" width="120" :show-overflow-tooltip="true"/>
<!--      <el-table-column label="状态 " align="center" prop="status" width="120" :show-overflow-tooltip="true">
        &lt;!&ndash;  0：关闭  1：未开始  2：正在进行 3:进行且停止下单 4：已结束 &ndash;&gt;
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==0">关闭</el-tag>
          <el-tag v-if="scope.row.status==1" type="info">未开始</el-tag>
          <el-tag v-if="scope.row.status==2" type="success">正在进行</el-tag>
          <el-tag v-if="scope.row.status==3" type="warning">进行且停止下单</el-tag>
          <el-tag v-if="scope.row.status==4" type="danger">已结束</el-tag>
&lt;!&ndash;          <dict-tag :options="dict.type.financial_line_stauts" :value="scope.row.status"/>&ndash;&gt;
        </template>
      </el-table-column>-->
      <!-- <el-table-column label="合同模板编码" align="center" prop="contractTemplateCode" /> -->
      <el-table-column label="限制用户等级" align="center" prop="limitUserLevel" width="120"
                       :show-overflow-tooltip="true"
      />
      <el-table-column label="是否上架" align="center" prop="status" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <!-- <dict-tag :options="dict.type.financial_status" :value="scope.row.status" /> -->
          <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
                     :inactive-value="2" @change="statusChange(scope.row)"
          />
        </template>
      </el-table-column>
<!--      <el-table-column label="开始时间" align="center" prop="startTime" width="180" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{m}/{d}/{y}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="周期结束时间" align="center" prop="finishTime" width="180" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, '{m}/{d}/{y}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="实际购买人数" align="center" prop="buyUser" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ scope.row.realityCount || 0 }}
        </template>
      </el-table-column>
      <el-table-column label="虚拟购买人数" align="center" prop="buyUser" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="购买人数上限" align="center" prop="realityCount" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ scope.row.dummyUser}}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <div>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                       v-hasPermi="['car:financial:edit']"
            >修改
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                       v-hasPermi="['car:financial:remove']"
            >删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />

    <!-- 添加或修改线上理财对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-position="left">
        <el-row>
          <el-col :span="12">
            <el-form-item label="理财名" prop="name">
              <el-input class="modal-input" v-model="form.name" placeholder="请输入理财名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最大金额" class="modal-input" prop="maxAmount">
              <el-input class="modal-input" v-model="form.maxAmount" placeholder="请输入最大金额"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="利率" prop="interestRate">
              <el-input class="modal-input" v-model="form.interestRate" placeholder="请输入利率"/>
              <span>%</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收益周期" prop="period">
              <el-input class="modal-input" v-model="form.period" placeholder="请输入收益周期"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="限制用户等级" prop="limitUserLevel">
              <el-select class="modal-input" v-model="form.limitUserLevel" @change="userLevelChange">
                <el-option v-for="(item, index) in userLevelList" :key="item.id" :label="item.name"
                           :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同模板编码" prop="contractTemplateCode">
              <!--                            <el-input class="modal-input" v-model="form.contractTemplateCode" placeholder="请输入合同模板编码" />-->
              <remote-select v-model="form.contractTemplateCode" :remote-api="contract" search-key="name"
                             value-key="code"
                             label-key="name" placeholder="请输入关键字" :init-data="contractList"
              />
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker class="modal-input" clearable v-model="form.startTime" type="date"
                              value-format="timestamp" placeholder="请选择开始时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="周期结束时间" prop="finishTime">
              <el-date-picker class="modal-input" clearable v-model="form.finishTime" type="date"
                              value-format="timestamp" placeholder="请选择周期结束时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="购买总人数" prop="dummyUser">
              <el-input type="number" :min="0" class="modal-input" v-model="form.dummyUser"
                        placeholder="请输入虚拟购买人数"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="虚拟购买人数" prop="buyUser">
              <el-input type="number" :min="0" class="modal-input" v-model="form.buyUser"
                        placeholder="请输入虚拟购买人数"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="封面图" prop="cover">
              <ImageUpload :limit="1" v-model="form.cover"></ImageUpload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFinancial,
  getFinancial,
  delFinancial,
  addFinancial,
  updateFinancial,
  changeStatus,
  contract,
  details
} from '@/api/financial/onlineList'
import { userLevelList } from '@/api/setting/gradeRebate'
import OssImagePreview from '@/components/ImagePreview/oss-image-preview.vue'
import DictSelect from '@/components/DictSelect/index.vue'
import RemoteSelect from '@/components/RemoteSelect/index.vue'

export default {
  name: 'Financial',
  components: { RemoteSelect, OssImagePreview, DictSelect },
  dicts: ['financial_line_stauts'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线上理财表格数据
      financialList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name:null,
        limitUserLevel:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: {
          required: true,
          message: '请输入理财产品名称',
          trigger: ['blur']
        },
        maxAmount: {
          required: true,
          message: '请输入最高余额要求',
          trigger: ['blur']
        },
        interestRate: {
          required: true,
          message: '请输入利率',
          trigger: ['blur']
        },
        period: {
          required: true,
          message: '请输入理财周期周期',
          trigger: ['blur']
        },
        limitUserLevel: {
          required: true,
          message: '请选择限制用户等级',
          trigger: ['blur']
        },
        contractTemplateCode: {
          required: true,
          message: '请选择合同模板',
          trigger: ['blur']
        },
        startTime: {
          required: true,
          message: '请选择理财开始时间',
          trigger: ['blur']
        },
        finishTime: {
          required: true,
          message: '请选择结束时间',
          trigger: ['blur']
        },
        cover: {
          required: true,
          message: '请上传封面图',
          trigger: ['blur']
        },
        dummyUser: {
          required: true,
          message: '请输入购买人数上限',
          trigger: ['blur']
        },
        buyUser: {
          required: true,
          message: '请输入虚拟购买人数',
          trigger: ['blur']
        }
      },
      //用户等级列表
      userLevelList: [],
      //可选合同列表
      contractList: []
    }
  },
  created() {
    this.getUserLevelList()
    this.getList()
    this.getContract()
  },
  methods: {
    contract: contract,
    /** 查询用户等级列表 */
    getContract() {
      contract().then(response => {
        this.contractList = response.data
      })
    },
    /** 查询用户等级列表 */
    getUserLevelList() {
      userLevelList().then(response => {
        this.userLevelList = response.data
      })
    },
    /** 查询线上理财列表 */
    getList() {
      this.loading = true
      listFinancial(this.queryParams).then(response => {
        this.financialList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.reset()
      this.open = false
    },
    // 表单重置
    reset() {
      this.form = {
        "id": null,
        "name": null,
        "cover": null,
        "maxAmount": null,
        "interestRate": null,
        "period": null,
        "contractTemplateCode": null,
        "limitUserLevel": null,
        "startTime": null,
        "finishTime": null,
        "buyUser": null,
        "dummyUser": null,
        "status": null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加线上理财'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      console.log(row)
      /* this.form = row;
      this.form.startTime =  Date.parse(this.form.startTime);
      this.form.finishTime =  Date.parse(this.form.finishTime);
      this.open = true;
      this.title = "修改线上理财"; */
      details(row.id).then(response => {
        this.form = response.data
        this.form.interestRate = this.form.interestRate.toFixed(2)*100;
        this.form.startTime = Date.parse(this.form.startTime)
        this.form.finishTime = Date.parse(this.form.finishTime)
        this.open = true
        this.title = '修改线上理财'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            // if (this.form.maxAmount) {
            //     this.$modal.msgSuccess("需要最大余额不能低于最低余额需要");
            // } else {
            //     return false;
            // }
            updateFinancial(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addFinancial(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除线上理财编号为"' + ids + '"的数据项？').then(function() {
        return delFinancial(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    //格式化金额
    formatter: function(row, key) {
      return this.$yun.priceFormatter(row[key])
    },
    //筛选购买的用户等级
    queryParamsULChange: function(value) {
      this.queryForm.limitUserLevel = value
    },
    //设置购买的用户等级
    userLevelChange: function(value) {
      this.form.limitUserLevel = value
    },
    //修改上架
    statusChange: function(row) {
      changeStatus(row.id).then(response => {
        this.$modal.msgSuccess('修改成功')
        this.getList()
      })
    },
    //筛选理财状态
    financialStatusChange: function(row) {

    }
  }
}
</script>
